/**
 * futures : masterlink.futures.controller.IndexController.java
 */
package masterlink.futures.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import masterlink.futures.dao.GroupDao;
import masterlink.futures.util.WebUtils;
import masterlink.futures.vo.Employee;
import masterlink.futures.vo.Group;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import baytony.util.Util;

/**
 * TODO Write the description
 *
 * @author : tonyyang
 * Create date : 2011/9/30
 * @version 1.0.0	
 */
@Controller
@RequestMapping(value = "/menu")
public class MenuController extends BasicController{

	@Autowired
	@Qualifier("GroupDao")
	private GroupDao groupDao;
	
	@RequestMapping(value = "/index")
	public String menu(HttpServletRequest req){
		Employee emp=WebUtils.getLogEmployee(req);
		log.trace("Employee {} loading menu",emp);
		if(Util.isEmptyList(emp.getAllGroups())){
			log.trace("Group is null, reloading groups for {}",emp);
			emp.setAllGroups(groupDao.findByEmp(emp.getEmpno()));
			WebUtils.setGroup(getIndexGroup(emp.getAllGroups()), req);
		}
		return "menu/menu";
	}
	
	@RequestMapping(value = "/reload")
	public String reload(HttpServletRequest req){ 
		Employee emp=WebUtils.getLogEmployee(req);
		log.trace("Clear groups for {}",emp);
		emp.setAllGroups(null);
		return menu(req);
	}

	private Group getIndexGroup(List<Group> all){
		if(!Util.isEmptyList(all)){
			return all.get(0);
		}
		return null;
	}
	
}
